package com.boboPlanet.comm.context;


import com.boboPlanet.comm.entity.po.LoginUserInfoPO;
import com.boboPlanet.comm.enums.LoginUserErrorEnum;
import com.boboPlanet.comm.exception.BusinessException;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.core.NamedThreadLocal;

/**
 * @Description: TODO 用户上下文信息
 * @Author: mis_wu
 * @Date: 2022/7/19
 **/
public class LoginUserContext {

    private static final String LOGIN_USER_KEY = "login-user";

    public static final ThreadLocal<LoginUserInfoPO> LOGIN_USER_INFO = new NamedThreadLocal<>(LOGIN_USER_KEY);

//    private static final InheritableThreadLocal<LoginUserInfoPO> inheritableThreadLocal = new InheritableThreadLocal<>();

    public static LoginUserInfoPO getLoginUserInfoPO(){
        return LOGIN_USER_INFO.get();
    }

    public static void mock(LoginUserInfoPO LoginUserInfoPO){
        if (ObjectUtils.isEmpty(LoginUserInfoPO)){
            throw new BusinessException(LoginUserErrorEnum.USER_LOGIN_INFO_NOT_NULL.getDesc());
        }
        LOGIN_USER_INFO.set(LoginUserInfoPO);
    }

    public static void clear(){
        LOGIN_USER_INFO.remove();
    }

}
